<template>
    <div class="addAirPlan">
        <custom-dialog title="批量控制" size="large" @dialogClose="dialogClose">
            <div class="topStep">
                <div class="warp">
                    <img src="@/assets/images/1021/moshi.png" />
                    <label class="title">模式</label>
                    <span :class="modeValue == item ? 'hover' : ''" v-for="item in modeList" :key="item" @click="modeValue = item">{{
                        item
                    }}</span>
                </div>
                <div class="warp">
                    <img src="@/assets/images/1021/wendu.png" />
                    <label class="title">设置温度</label>
                    <div style="font-size: 30px; color: #409eff">
                        <div class="wdControl" @click="reduce()">
                            <img src="@/assets/images/1021/reduce.svg" />
                        </div>
                        <div class="wdControl">{{ defaultValue }}</div>
                        <div class="wdControl" @click="plus()">
                            <img src="@/assets/images/1021/add.svg" />
                        </div>

                        <span :class="wdWdz == 1 ? 'hover' : ''" @click="wdwdzClick">无动作</span>
                    </div>
                </div>
                <div class="warp">
                    <img src="@/assets/images/1021/fengsu.png" />
                    <label class="title">风速</label>
                    <span
                        :class="windSpeedValue == item ? 'hover' : ''"
                        v-for="item in windSpeedList"
                        :key="item"
                        @click="windSpeedValue = item"
                        >{{ item }}</span
                    >
                </div>
                <div class="warp">
                    <img src="@/assets/images/1021/fengxiang.png" />
                    <label class="title">风向</label>
                    <span
                        :class="windDirValue == item ? 'hover' : ''"
                        v-for="item in windDirList"
                        :key="item"
                        @click="windDirValue = item"
                        >{{ item }}</span
                    >
                </div>
                <div class="warp">
                    <img src="@/assets/images/1021/kaiguan.png" />
                    <label class="title">开关</label>
                    <span
                        :class="openCloseValue == item ? 'hover' : ''"
                        v-for="item in openCloseList"
                        :key="item"
                        @click="openCloseValue = item"
                        >{{ item }}</span
                    >
                </div>
                <!-- <div class="warp">
                    <img src="@/assets/images/1021/jijie.png" />
                    <label class="title">季节选择</label>
                    <span :class="seasonValue == item ? 'hover' : ''" v-for="item in seasonList" :key="item" @click="seasonValue = item">{{
                        item
                    }}</span>
                </div>
                <div class="warp">
                    <img src="@/assets/images/1021/kongtiaokaiji.png" />
                    <label class="title">空调开机</label>
                    <span
                        :class="airControlValue == item ? 'hover' : ''"
                        v-for="item in airControlList"
                        :key="item"
                        @click="airControlValue = item"
                        >{{ item }}</span
                    >
                </div> -->
            </div>
            <div style="text-align: right">
                <el-button type="primary" class="nextBtn" :loading="loading" @click="dqClick" v-if="nodeList.length == 1">读取</el-button>
                <el-button type="primary" class="nextBtn" @click="saveData">确定</el-button>
            </div>
        </custom-dialog>
    </div>
</template>

<script>
import customDialog from '@/components/customComponents/customDialog'
import {PopupCommon} from '@/libs/popupCommon'
import iconBorder from '@/components/newIndex/iconBorder'
import {AjaxCommon} from '@/libs/apiCommon'
export default {
    name: 'addAirPlan',
    props: ['treeList'],
    watch: {
        treeList: {
            handler(newV) {
                if (newV) {
                    this.nodeList = newV
                }
            },
            immediate: true
        }
    },
    components: {customDialog, iconBorder},
    data() {
        return {
            loading: false,
            nodeList: [],
            wdWdz: 1,
            defaultValue: 16,
            modeValue: '无动作',
            // 空调模式
            modeList: ['自动', '制冷', '除湿', '送风', '制热', '无动作'],
            modeValueList: [0, 1, 2, 3, 4, 255],
            windSpeedValue: '无动作',
            // 风速 Sjx=6  cs参数设定对应  0-自动 1-低速 2-中速 3-高速
            windSpeedList: ['自动', '1档', '2档', '3档', '无动作'],
            windSpeedValueList: [0, 1, 2, 3, 255],
            openCloseValue: '无动作',
            // openCloseList: ['强制开', '强制关', '复位', '手动开', '手动关', '无动作'],
            openCloseList: ['强制开', '强制关', '无动作'],
            // sjx参数项传3   cs参数  开关机指令  0关  1开
            openCloseValueList: [1, 0, 255],
            airControlValue: '无动作',
            airControlList: ['不允许', '按策略运行', '无条件允许', '无动作'],
            windDirValue: '无动作',
            windDirList: ['自动摆风', '手动摆风', '无动作'],
            // 风向 Sjx=7  cs参数设定对应  0-自动摆风 : 1-手动摆风
            windDirValueList: [0, 1, 255],
            seasonValue: '无动作',
            seasonList: ['夏季', '冬季', '无动作'],
            tempValue: 0
        }
    },
    computed: {},
    created() {},
    methods: {
        getValue(str) {
            if (str?.indexOf('|') != -1) {
                const data = str.split('|')
                const params = {
                    总用电量: data[0],
                    信号强度: data[1],
                    电流Ia: data[2],
                    电压Va: data[3],
                    功率Pa: data[4],
                    开关机: data[5],
                    设定模式: data[6],
                    环境温度: data[7],
                    设定温度: data[8],
                    设定风速: data[9],
                    空调风向: data[10],
                    控制模式: data[11],
                    季节模式: data[12],
                    空调插头状态: data[13],
                    允许空调开机: data[14],
                    温度使能状态: data[15],
                    设备时间: data[16],
                    /** 调温 人感  温度  时间  手动 计量 断电 机房省电 定时*/
                    工作状态: data[17],
                    /** 1 有故障  断电  过流  通讯模块故障 红外模块故障 状态检测故障 时间参数错误 温度参数错误 人感参数错误 待机功耗异常 实时时钟故障 温度检测故障 空调脱机运行 存储参数错误 预存电量用完 存储电量错误 存储校表错误*/
                    故障状态: data[18]
                }
                // this.ktInfo = params

                // 空调模式
                switch (params.设定模式) {
                    case 0:
                        this.modeValue = '自动'
                        break
                    case 1:
                        this.modeValue = '制冷'
                        break
                    case 2:
                        this.modeValue = '除湿'
                        break
                    case 3:
                        this.modeValue = '送风'
                        break
                    case 4:
                        this.modeValue = '制热'
                        break
                    default:
                        this.modeValue = '无动作'
                        break
                }
                // 风速
                switch (params.设定风速) {
                    case 0:
                        this.windSpeedValue = '自动'
                        break
                    case 1:
                        this.windSpeedValue = '低速'
                        break
                    case 2:
                        this.windSpeedValue = '中速'
                        break
                    case 3:
                        this.windSpeedValue = '高速'
                        break
                    default:
                        this.windSpeedValue = '无动作'
                        break
                }

                // 设定温度 ktDetail.设定温度
                this.defaultValue = params.设定温度
                // 开关
                switch (params.开关机) {
                    case 0:
                        this.windSpeedValue = '强制关'
                        break
                    case 1:
                        this.windSpeedValue = '强制开'
                        break
                    default:
                        this.windSpeedValue = '无动作'
                        break
                }
                // 风向 空调风向
                switch (params.空调风向) {
                    case 0:
                        this.windSpeedValue = '自动摆风'
                        break
                    case 1:
                        this.windSpeedValue = '手动摆风'
                        break
                    default:
                        this.windSpeedValue = '无动作'
                        break
                }
            }
            this.loading = false
        },
        async getDeviceStatus(params) {
            return await AjaxCommon(params, 'post', {})
        },
        async dqClick() {
            this.loading = true
            // 获取设备基本信息
            const deviceInfo = this.nodeList[0]

            // 进入页面先加载数据库中已有数据，再通过调用GetEqRtData（可页面设置定时器调用3次，返回数据即更新用量，无返回变更信号状态为灰色，用量为GetEQRTInfo接口数据）获取实时数据
            const params = `/api/Eq/GetEqRtData?eqid=${deviceInfo.eq_guid}`
            /** 	设备实时用量 返回string 字符 实时电量或水量等数据，多个参数用|分隔 如 返回-1说明终端通迅异常网关正常 返回 ""或null 网关和终端通迅异常 */
            let actData = await this.getDeviceStatus(params)
            let value = ''
            value = actData.data?.Message
            if (value && value != '-1') {
                this.getValue(value)
                return
            }
            setTimeout(async () => {
                actData = await this.getDeviceStatus(params)
                value = actData.data?.Message
                if (value && value != '-1') {
                    this.getValue(value)
                    this.$emit('showSpin', false)
                    return
                }
                setTimeout(async () => {
                    actData = await this.getDeviceStatus(params)
                    value = actData.data?.Message
                    this.$emit('showSpin', false)
                    if (actData.data?.Message == '' || actData.data?.Message == null) {
                        this.loading = false
                        PopupCommon.notify('info', '网关和终端通迅异常')
                        return
                    }
                    if (actData.data?.Message == -1) {
                        this.loading = false
                        PopupCommon.notify('info', '终端通迅异常网关正常')
                        return
                    }
                    if (value && value != '-1') {
                        this.getValue(value)
                    }
                }, 5000)
            }, 5000)
        },
        saveData() {
            // 模式
            const msIndex = this.modeList.findIndex((t) => t == this.modeValue)
            const msValue = this.modeValueList[msIndex]
            // 设置温度
            let wdValue = 0
            if (this.wdWdz == 1) {
                wdValue = 255
            } else {
                wdValue = this.defaultValue
            }
            // 风速
            const sfIndex = this.windSpeedList.findIndex((t) => t == this.windSpeedValue)
            const sfValue = this.windSpeedValueList[sfIndex]
            // 风向
            const fxIndex = this.windDirList.findIndex((t) => t == this.windDirValue)
            const fxValue = this.windDirValueList[fxIndex]
            // 开关
            const kgIndex = this.openCloseList.findIndex((t) => t == this.openCloseValue)
            const kgValue = this.openCloseValueList[kgIndex]

            const finishValue = kgValue + '|' + msValue + '|' + wdValue + '|' + sfValue + '|' + fxValue
            this.$emit('save', finishValue)
        },
        wdwdzClick() {
            if (this.wdWdz == 0) {
                this.wdWdz = 1
            } else {
                this.wdWdz = 0
            }
        },
        reduce() {
            if (this.defaultValue <= 16) {
                this.defaultValue = 16
            } else {
                this.defaultValue--
            }
            this.wdWdz = 0
        },
        plus() {
            if (this.defaultValue >= 31) {
                this.defaultValue = 31
            } else {
                this.defaultValue++
            }
            this.wdWdz = 0
        },
        dialogClose() {
            this.$emit('dialogClose', '')
        }
    }
}
</script>

<style lang="scss" scoped>
.ivu-tabs-bar {
    margin-bottom: 0 !important;
}
.topStep {
    width: 100%;
    overflow: hidden;
    display: flex;
    align-items: center;
    gap: 20px;
    flex-wrap: wrap;
    .warp {
        width: calc(60% - 20px);
        display: flex;
        background-color: #f0f3fa;
        border-radius: 8px;
        padding: 10px;
        align-items: center;
        justify-content: flex-start;
        &:nth-child(2n) {
            width: calc(40% - 20px);
        }
        .title {
            color: #2f4bbd;
            margin-left: 10px;
            margin-right: 30px;
            font-size: 18px;
        }
        span {
            display: flex;
            color: #2f4bbd;
            font-size: 16px;
            margin-left: 10px;
            cursor: pointer;
            border: 1px solid #f0f3fa;
            padding: 6px;
            display: inline-block;
            &:hover {
                background-color: #dce3f4;
                border-color: #2f4bbd;
                border-radius: 4px;
            }
        }
        .hover {
            background-color: #dce3f4;
            border-color: #2f4bbd;
            border-radius: 4px;
        }
    }
}
.wdControl {
    display: inline-block;
    border: 1px solid #ddd;
    padding: 4px 10px;
    font-size: 22px;
    color: #5795d4;
    cursor: pointer;
    line-height: 28px;
    height: 35px;
}
.wdControl:nth-child(1) {
    border-top-left-radius: 6px;
    border-bottom-left-radius: 6px;
    width: 40px;
    background-color: #dbe7f5;
}
.wdControl:nth-child(3) {
    border-top-right-radius: 6px;
    border-bottom-right-radius: 6px;
    width: 40px;
    background-color: #dbe7f5;
}
.wdControl:nth-child(2) {
    border-right: none;
    border-left: none;
}
</style>